package com.leyou.item.service.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import com.leyou.common.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author: wangzibeng
 * Date: 2019/4/28 0028
 * Time: 12:48
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDTO> queryCategoryByPid(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        List<Category> categoryList = categoryMapper.select(category);
        if (CollectionUtils.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        //转换为CategoryDTO 集合
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }


    @Override
    public Long addCategory(CategoryDTO categoryDTO) {
        Category category = BeanHelper.copyProperties(categoryDTO, Category.class);
        int insertCount = categoryMapper.insertSelective(category);
        if (insertCount != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
        return category.getId();
    }

    @Override
    public void deleteCategory(Long id) {
        Category category = categoryMapper.selectByPrimaryKey(id);
        //判断是不是父节点
        if (category.getIsParent()) {
            //先删除所有的子节点 delete from tb where parent_id = id
            Category delCategory = new Category();
            delCategory.setParentId(id);
            categoryMapper.delete(delCategory);
        }
        //在删除父节点
        int i = categoryMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    @Override
    public void editCategoryById(Long id, String name) {
        Category category = new Category();
        category.setName(name);
        category.setId(id);
        int updateCount = categoryMapper.updateByPrimaryKeySelective(category);
        if (updateCount != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public List<CategoryDTO> getCategoryByBrand(Long id) {
        //获取所有的分类id
        int[] categoryIds = categoryMapper.getCategoryIds(id);
        ArrayList<Category> categoryList = new ArrayList<>();
        for (int categoryId : categoryIds) {
            //查询所有的分类对象
            Category category = categoryMapper.selectByPrimaryKey(categoryId);
            categoryList.add(category);
        }
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }

    @Override
    public List<CategoryDTO> queryCategoryByIds(List<Long> IdList) {
        List<Category> categoryList = categoryMapper.selectByIdList(IdList);
        if (CollectionUtils.isEmpty(categoryList)) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(categoryList, CategoryDTO.class);
    }

    @Override
    public List<CategoryDTO> queryCategoryByCid3(Long cid3) {
        if (cid3 == null) {
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        Category category3 = categoryMapper.selectByPrimaryKey(cid3);
        if (category3 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        Category category2 = categoryMapper.selectByPrimaryKey(category3.getParentId());
        if (category2 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        Category category1 = categoryMapper.selectByPrimaryKey(category2.getParentId());
        if (category1 == null) {
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        List<Category> categories = Arrays.asList(category1, category2, category3);
        return BeanHelper.copyWithCollection(categories, CategoryDTO.class);
    }
}
